# -*- coding: utf-8 -*-
"""
Created on Thu May 25 20:38:23 2017

@author: Aeen
"""
import os
from functools import reduce

title = '''<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN">
  <head>
    <title>测试</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <link href="stylesheet.css" rel="stylesheet" type="text/css"/>
  </head>
  <body>
'''


def split_txt(html):
    txt = open(html, 'r', encoding='utf-8')
    lst_txt = txt.read().split('。')
    i = 0
    z = []
    while i <= len(lst_txt)//3-1:
        try:
            z.append(reduce(lambda x,y: x+y, lst_txt[i*3:i*3+3]))
        except IndexError as e:
            print(e)
            z.append(reduce(lambda x,y: x+y, lst_txt[i*3:len(lst_txt)-i*3]))
        i = i + 1
    txt.close()
    return z

lst_dir = ['H:\\txt\\' + y for y in os.listdir('H:\\txt')]
for i, x in enumerate(lst_dir):
    html = x[:7] + str(i + 1) + '.txt'
#    lst = split_txt(html)
    path = 'H:\\html\\' + str(i + 1) + '.html'
    with open(path, 'w', encoding='utf-8') as html_con:
        html_con.write(title)
        intial = ''''''
        for i in split_txt(html):
            intial = intial + '\n\t<p>%s。</p>' % (i)
        html_con.write(intial)
        html_con.write('''\n  </body>\n</html>''')
    print('{:>2} {:>}'.format(str(lst_dir.index(x) + 1), 'is ok'))
